#ifndef _MBOARD_H_
#define _MBOARD_H_

#include <stdint.h>

typedef struct {
	uint32_t ahb;
	uint32_t apb1;
	uint32_t apb2;
	uint32_t apb1_tim;
	uint32_t apb2_tim;
} MBoardClk_t;

typedef struct {
	MBoardClk_t clk;
	volatile uint32_t ms;
} MBoardInfo_t;

extern MBoardInfo_t mboardInfo;

void mBoardInit(void);
void mBoardGoIdle(void);
void mBoardDelayMs(uint16_t ms);
void mBoardDelayS(uint16_t s);
void mBoardTest(void);


/*********************** LED **********************/
typedef enum {
	MLED_COLOR_RED   = 0x1U<<9,
	MLED_COLOR_GREEN = 0x1U<<10,
	MLED_COLOR_ALL   = 0x3U<<9,
} MLedColor_e;

typedef enum {
	MLED_STATE_OFF    = 0,
	MLED_STATE_ON     = 1,
	MLED_STATE_TOGGLE = 2,
} MLedState_e;

void mLedSet(MLedColor_e color, MLedState_e state);
void mLedTest(void);


/*********************** KEY **********************/
typedef enum {
	MKEY_STATE_PRESS   = 0,
	MKEY_STATE_RELEASE = 1,
} MKeyState_e;

typedef struct {
	volatile union {
		struct {
			uint8_t up    : 1;
			uint8_t down  : 1;
			uint8_t left  : 1;
			uint8_t right : 1;
		};
		uint8_t all;
	} state;  /* 当前按键的状态 */
	volatile struct {
		uint8_t up;
		uint8_t down;
		uint8_t left;
		uint8_t right;
	} count; /* 按键按下去的次数 */
} MKeyInfo_t;

extern MKeyInfo_t mkeyInfo;

void mKeyTest(void);


/********************* TIM BASE *******************/
void mTimBaseTest(void);


/*********************** DMA **********************/
void mDmaTest(void);


/********************** USART *********************/
void mUsartTxByte(uint8_t ch);
void mUsartTxBytes(const uint8_t *bytes, uint32_t len);
void mUsartTxString(const char *str);
uint8_t mUsartRxByte(void);
uint32_t mUsartRxBytes(uint8_t *bytes, uint32_t maxLen);
void mUsartTxWaitFinish(void);
void mUsartRxClear(void);
void mUsartTest(void);


/********************** SD卡 *********************/
void mSdioSdInit(void);
void mSdioSdRead(void *buf, uint32_t start, uint32_t blocks);
void mSdioSdWrite(const void *buf, uint32_t start, uint32_t blocks);
void mSdioSdCtrl(uint8_t cmd, void *buf);
void mSdioTest(void);


/******************* SPI Flash *******************/
void mSpiFlashInfo(void);
void mSpiFlashRead(uint32_t addr, uint8_t *buf, uint32_t len);
void mSpiFlashWrite(uint32_t addr, const uint8_t *buf, uint32_t len);
void mSpiFlashErase(uint32_t addr, uint32_t len);
void mSpiTest(void);


/******************* I2C EEPROM ******************/
void mI2cEepromRead(uint32_t addr, uint8_t *buf, uint32_t len);
void mI2cEepromWrite(uint32_t addr, const uint8_t *buf, uint32_t len);
void mI2cWaitFinish(void);
void mI2cTest(void);


/******************** FSMC SRAM ******************/
void mFsmcTest(void);

#endif /* _MBOARD_H_ */
